package com.demandbreakdown.xiyun.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.demandbreakdown.xiyun.common.annotation.ApiLog;
import com.demandbreakdown.xiyun.common.base.Result;
import com.demandbreakdown.xiyun.common.base.ResultUtils;
import com.demandbreakdown.xiyun.model.dto.picture.PictureQueryDTO;
import com.demandbreakdown.xiyun.model.dto.picture.PictureReview;
import com.demandbreakdown.xiyun.model.dto.picture.PictureUpdateDTO;
import com.demandbreakdown.xiyun.model.dto.picture.PictureUploadDTO;
import com.demandbreakdown.xiyun.model.vo.common.PageVO;
import com.demandbreakdown.xiyun.model.vo.picture.PictureVO;
import com.demandbreakdown.xiyun.service.PictureService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图片管理控制器
 *
 * @author poxz
 */
@RestController
@RequestMapping("/picture")
@RequiredArgsConstructor
@Tag(name = "图片管理", description = "图片上传、查看、管理相关接口")
public class PictureController {

    private final PictureService pictureService;

    @ApiLog("上传图片")
    @Operation(summary = "上传图片", description = "上传图片文件到服务器，并创建图片记录")
    @PostMapping
    @SaCheckLogin
    public Result<PictureVO> uploadPicture(
            @Parameter(description = "图片文件", required = true)
            @RequestParam("file") MultipartFile file,
            @Parameter(description = "图片元数据")
            @Valid PictureUploadDTO uploadDTO) {
        Long userId = StpUtil.getLoginIdAsLong();
        PictureVO pictureVO = pictureService.uploadPicture(userId, file, uploadDTO);
        return ResultUtils.success(pictureVO);
    }


    @ApiLog("分页查询图片")
    @Operation(summary = "分页查询图片", description = "根据条件分页查询图片列表")
    @GetMapping
    public Result<PageVO<PictureVO>> getPictures(PictureQueryDTO pictureQueryDTO) {
        Long currentUserId = null;
        if (StpUtil.isLogin()) {
            currentUserId = StpUtil.getLoginIdAsLong();
        }
        PageVO<PictureVO> pageVO = pictureService.getPictures(pictureQueryDTO, currentUserId);
        return ResultUtils.success(pageVO);
    }

    @ApiLog("查看图片详情")
    @Operation(summary = "查看图片详情", description = "查看图片详细信息，会增加浏览次数")
    @GetMapping("/{id}")
    public Result<PictureVO> getPictureById(@Parameter(description = "图片ID", required = true) @PathVariable Long id) {
        Long currentUserId = null;
        if (StpUtil.isLogin()) {
            currentUserId = StpUtil.getLoginIdAsLong();
        }
        PictureVO pictureVO = pictureService.getPictureById(id, currentUserId);
        return ResultUtils.success(pictureVO);
    }

    @ApiLog("编辑图片信息")
    @Operation(summary = "编辑图片信息", description = "修改图片名称、简介、分类、标签等信息")
    @PutMapping("/{id}")
    @SaCheckLogin
    public Result<PictureVO> updatePicture(
            @Parameter(description = "图片ID", required = true) @PathVariable Long id,
            @Valid @RequestBody PictureUpdateDTO pictureUpdateDTO) {
        Long userId = StpUtil.getLoginIdAsLong();
        PictureVO pictureVO = pictureService.updatePicture(id, userId, pictureUpdateDTO);
        return ResultUtils.success(pictureVO);
    }

    @ApiLog("删除图片")
    @Operation(summary = "删除图片", description = "逻辑删除图片，异步队列24h后真正删除OSS")
    @DeleteMapping("/{id}")
    @SaCheckLogin
    public Result<Void> deletePicture(@Parameter(description = "图片ID", required = true) @PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        pictureService.deletePicture(id, userId);
        return ResultUtils.success();
    }

    @ApiLog(value = "下载图片", recordResult = false)
    @Operation(summary = "下载图片", description = "生成图片下载链接，有IP限流")
    @GetMapping("/{id}/download")
    public void downloadPicture(
            @Parameter(description = "图片ID", required = true) @PathVariable Long id,
            HttpServletResponse response) {
        Long currentUserId = null;
        if (StpUtil.isLogin()) {
            currentUserId = StpUtil.getLoginIdAsLong();
        }
        pictureService.downloadPicture(id, currentUserId, response);
    }

    @ApiLog("分享图片")
    @Operation(summary = "分享图片", description = "增加图片分享次数")
    @PostMapping("/{id}/share")
    public Result<Void> sharePicture(@Parameter(description = "图片ID", required = true) @PathVariable Long id) {
        Long currentUserId = null;
        if (StpUtil.isLogin()) {
            currentUserId = StpUtil.getLoginIdAsLong();
        }
        pictureService.sharePicture(id, currentUserId);
        return ResultUtils.success();
    }

    @ApiLog("审核图片")
    @Operation(summary = "审核图片", description = "管理员审核图片，通过后图片才公开")
    @PatchMapping("/admin/{id}/review")
    @SaCheckRole("admin")
    public Result<Void> reviewPicture(
            @Parameter(description = "图片ID", required = true) @PathVariable Long id,
            @Valid @RequestBody PictureReview pictureReview) {
        Long reviewerId = StpUtil.getLoginIdAsLong();
        pictureService.reviewPicture(id, reviewerId, pictureReview);
        return ResultUtils.success();
    }
}
